package com.travelexp;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

public class DataProvider extends ContentProvider{
	public static final String CONTENT_PATH = "com.travelexp.dataprovider";
	public static final String AUTHORITY = "content://" + CONTENT_PATH;
	public static final Uri CONTENT_URI = Uri.parse(AUTHORITY);
	public static final String TABLE = "table";
	private TravelData travelData;
	
	@Override
	public boolean onCreate() {
		travelData = new TravelData(getContext());
		return false;
	}
	
	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// Updating the table travel
		long id = -1;
		
		if(uri.toString().contains("/" + TravelData.T_TRAVEL))
		{
			id = travelData.insertTravel(values);
		}
		
		// The id of the inserted row
		if(id != -1) return Uri.withAppendedPath(uri, Long.toString(id));
		else return uri;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		return 0;
	}	
	
	@Override
	public int delete(Uri arg0, String arg1, String[] arg2) {
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		if(uri.getLastPathSegment() == null)
		{
			return "vnd.android.cursor.item/vnd." + CONTENT_PATH;
		}
		else
		{
			return "vnd.android.cursor.dir/vnd." + CONTENT_PATH;
		}
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) 
	{	
		String table = null;
		String uriString = uri.toString();
		if(uriString.contains(TravelData.T_IMAGE))
		{
			table = TravelData.T_IMAGE;
		}
		else if(uriString.contains(TravelData.T_LOCAL))
		{
			table = TravelData.T_LOCAL;
		}
		else if(uriString.contains(TravelData.T_TRAVEL))
		{
			table = TravelData.T_TRAVEL;
		}
		else
		{
			return null;
		}
		
		Cursor c = travelData.execQuery(table, projection, selection, 
									selectionArgs, sortOrder);
		return c;
	}


}
